
<template>
  <div id="todo">
    <div class="control">
      <a-input placeholder="请输入任务" class="my_ipt" @change="changInput" />
      <a-button type="primary" @click="addListItem">添加事项</a-button>
    </div>

    <a-list bordered :dataSource="infolist" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="changeState($event,item)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeListItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{doneCount}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="keyWords==='all' ? 'primary' : 'default'" @click="controlType('all')">全部</a-button>
          <a-button
            :type="keyWords==='undone' ? 'primary' : 'default'"
            @click="controlType('undone')"
          >未完成</a-button>
          <a-button
            :type="keyWords==='done' ? 'primary' : 'default'"
            @click="controlType('done')"
          >已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  created() {
    this.$store.dispatch('getList', 'inputVal')
  },
  computed: {
    ...mapState(['list', 'keyWords']),
    ...mapGetters(['doneCount', 'infolist'])
  },
  methods: {
    changInput(e) {
      this.$store.commit('changeInput', e.target.value)
    },
    addListItem() {
      if (this.$store.state.inputVal.trim().length === 0) {
        return this.$message.warning('数据不可为空')
      }
      this.$store.commit('addListItem')
    },
    removeListItem(id) {
      this.$store.commit('removeListItem', id)
    },
    changeState($event, item) {
      const row = {
        id: item.id,
        done: $event.target.checked
      }
      this.$store.commit('changeState', row)
    },
    clear() {
      this.$store.commit('clearDone')
    },
    controlType(type) {
      this.$store.commit('controlType', type)
    }
  }
}
</script>

<style scoped>
#todo {
  padding: 10px;
}

.control {
  margin-bottom: 20px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
  margin: 0 auto;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
